package com.onetime.mall.trading.controller;

import com.onetime.mall.trading.constants.ErrorMsg;
import com.onetime.mall.trading.entity.OrderAddressEntity;
import com.onetime.mall.trading.service.OrderAddressService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Map;


/**
 * 订单地址表
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-26 19:38:29
 */
@RestController
@RequestMapping("order-address")
public class OrderAddressController {
    @Autowired
    private OrderAddressService orderAddressService;


    /**
     * 为订单添加地址信息
     *
     * @param shUserId           用户id
     * @param orderAddressEntity 订单地址
     * @return
     */
    @PostMapping("/add")
    public R addOrderAddress(@RequestParam("userId")
                             @NotNull(message = "登录异常 请重新登录")
                             @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                             @RequestBody OrderAddressEntity orderAddressEntity) {
        boolean address = orderAddressService.addOrderAddress(orderAddressEntity);
        return R.ok().put("data", address);
    }

    /**
     * 获得订单地址
     *
     * @param shUserId 用户id
     * @param orderId  订单id
     * @return
     */
    @GetMapping("/info")
    public R getOrderAddress(@RequestParam("userId")
                             @NotNull(message = "登录异常 请重新登录")
                             @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                             @RequestParam Long orderId) {
        OrderAddressEntity orderAddress = orderAddressService.getOrderAddress(orderId);
        return R.ok().put("data", orderAddress);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OrderAddressEntity orderAddress = orderAddressService.getById(id);

        return R.ok().put("orderAddress", orderAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderAddressEntity orderAddress) {
        orderAddressService.save(orderAddress);

        return R.ok();
    }

    @PostMapping("/update")
    public R updateOrderAddress(@RequestParam("userId")
                                @NotNull(message = "登录异常 请重新登录")
                                @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                                @RequestBody OrderAddressEntity orderAddressEntity) {
        boolean address = orderAddressService.updateOrderAddress(orderAddressEntity);
        if (address) {
            return R.ok().put("data", orderAddressEntity);
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    /**
     * 修改
     */
    @RequestMapping("/update1")
    public R update(@RequestBody OrderAddressEntity orderAddress) {
        orderAddressService.updateById(orderAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        orderAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
